package advanced.datastructure;

import structure5.Association;

public class AreaCodeCounter {
    public MyVector<Association> areaCodeCounts = new MyVector<Association>();
    
	public void updateCount(String newAreaCode)
	{
		if(newAreaCode.length()!=3)
		{
			return;
		}
		int i;
		for (i = 0; i < areaCodeCounts.size(); i++)
		{
			Association araCodeCount;
			String address;
			
			// get the association
			araCodeCount = (Association)areaCodeCounts.get(i);
			// get the area code from the association
			address = (String)araCodeCount.getKey();
			
			if (address.equals(newAreaCode))
			{ 
				// match: increment integer in association
				Integer studentCount = (Integer)araCodeCount.getValue();
				araCodeCount.setValue(new Integer(studentCount.intValue() + 1));
				break;
			}
		}
		// mismatch: add area code, frequency 1.
		if (i == areaCodeCounts.size())
		{
			areaCodeCounts.add(new Association(newAreaCode,new Integer(1)));
		}
	}

	public Association getAreaCodeSharedByMost()
	{
		areaCodeCounts.sort(new AddressStudentsAssocComparator());
		return areaCodeCounts.getLast();
	}


}
